//
//  ForgetCell.swift
//  DollBuy
//
//  Created by 郑东喜 on 2016/11/18.
//  Copyright © 2016年 郑东喜. All rights reserved.
//

import UIKit


protocol ForgetCellDelegate {
    func choosePic(_com: (String?)) -> Void
}

class ForgetCell: UITableViewCell,UITextFieldDelegate {
    var delegate : ForgetCellDelegate?
    
    
    
    //手机号
    var tfNum: TfPlaceHolder = {
        let lab = TfPlaceHolder()
        lab.frame = CGRect(x: 50, y: 0, width: UIScreen.main.bounds.size.width - 60 , height: 45)
        //        lab.placeholder = "请输入您的手机号码"
        lab.plStrSize(str: "请输入您的手机号码", holderColor: UIColor.gray, textFontSize: 13)
        lab.tag = 111
        
        lab.text = ""
        
        //文字左对齐
        lab.textAlignment = NSTextAlignment.left
        return lab
    }()
    
    //验证码
    var tfAuto: TfPlaceHolder = {
        let lab = TfPlaceHolder()
        lab.frame = CGRect(x: 50, y: 0, width: UIScreen.main.bounds.size.width - 60 , height: 45)
        lab.plStrSize(str: "请输入您的验证码", holderColor: UIColor.gray, textFontSize: 13)
        
        lab.tag = 112
        
        
        //文字左对齐
        lab.textAlignment = NSTextAlignment.left
        return lab
    }()
    
    //密码
    var tfPass: TfPlaceHolder = {
        let lab = TfPlaceHolder()
        lab.frame = CGRect(x: 50, y: 0, width: UIScreen.main.bounds.size.width - 60 , height: 45)
        lab.plStrSize(str: "请输入您的密码", holderColor: UIColor.gray, textFontSize: 13)
        
        lab.tag = 113
        
        //文字左对齐
        lab.textAlignment = NSTextAlignment.left
        return lab
    }()
    
    //密码
    var tfConfirmPass: TfPlaceHolder = {
        let lab = TfPlaceHolder()
        lab.frame = CGRect(x: 50, y: 0, width: UIScreen.main.bounds.size.width - 60 , height: 45)
        lab.plStrSize(str: "请再次输入您的验证码", holderColor: UIColor.gray, textFontSize: 13)
        
        lab.tag = 114
        
        //文字左对齐
        lab.textAlignment = NSTextAlignment.left
        return lab
    }()
    
    
    
    //前置图片
    lazy var imgView: UIImageView = {
        let img = UIImageView()
        //自适应放大
        img.contentMode = UIViewContentMode.scaleAspectFit
        img.frame = CGRect(x: 7.5, y: 7.5, width: 30, height: 30)
        return img
    }()
    
    //分割线
    lazy var line : UIView = {
        let lin = UIView()
        lin.frame = CGRect(x: 0, y: 45, width: UIScreen.main.bounds.size.width, height: 0.5)
        lin.backgroundColor = UIColor.lightGray
        return lin
    }()
    
    //确定按钮
    lazy var btn : UIButton = {
        let btn = UIButton()
        btn.backgroundColor = UIColor.orange
        btn.setTitle("确定", for: .normal)
        btn.frame = CGRect(x: 15, y: 0, width: Int(SW - 30), height: 40)
        btn.addTarget(self, action: #selector(RigisterCell.confirmSEL), for: .touchUpInside)
        btn.layer.cornerRadius = 8
        return btn
    }()
    
    //验证码按钮
    lazy var yzmBtn : UIButton = {
        let btn = UIButton()
        btn.backgroundColor = UIColor.orange
        btn.frame = CGRect(x: SW - 15 - 100, y: 7.5, width: 100, height: 30)
        btn.backgroundColor = UIColor.orange
        btn.layer.cornerRadius = 8
        btn.setTitle("获取验证码", for: .normal)
        btn.addTarget(self, action: #selector(RigisterCell.autoSEL), for: .touchUpInside)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        
        return btn
    }()
    
    //重写构造方法
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        tfNum.delegate = self
        tfAuto.delegate = self
        tfPass.delegate = self
        tfConfirmPass.delegate = self
        
        //添加控件
        addSubview(tfNum)
        addSubview(tfAuto)
        addSubview(tfPass)
        addSubview(tfConfirmPass)
        
        addSubview(imgView)
        addSubview(line)
        addSubview(btn)
        addSubview(yzmBtn)
        
        //去除高亮效果
        self.selectionStyle = .none
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        delegate?.choosePic(_com: textField.text)
        
        switch textField.tag {
        case 111:
            localSave.set(textField.text, forKey: "one")
        case 112:
            localSave.set(textField.text, forKey: "two")
        case 113:
            localSave.set(textField.text, forKey: "three")
        case 114:
            localSave.set(textField.text, forKey: "four")
        default:
            break
            
            
        }
        
        print(textField.text as Any)
        
        return true
    }
    

    
//    func textFieldDidEndEditing(_ textField: UITextField) {
//        
//        
//        delegate?.choosePic(_com: textField.text)
//        
//        switch textField.tag {
//        case 111:
//            localSave.set(textField.text, forKey: "one")
//        case 112:
//            localSave.set(textField.text, forKey: "two")
//        case 113:
//            localSave.set(textField.text, forKey: "three")
//        case 114:
//            localSave.set(textField.text, forKey: "four")
//        default:
//            break
//            
//            
//        }
//        
//        print(textField.text as Any)
//    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
    
}

// MARK:- 单击事件
extension ForgetCell {
    //确定事件
    func confirmSEL() -> Void {
        print("单机确定")
        
       print(localSave.object(forKey: "one") as Any)
        
        ForgetModel.shared.forgetPass(tfNum: tfNum, tfAuto: tfAuto, tfPass: tfPass, tfConfirmPass: tfConfirmPass) { (changePassStatus) in
            print(changePassStatus)
        }
    }
    
    //验证码事件
    func autoSEL() -> Void {
        print("单机验证码阿牛")
//        SendAutoModel.shared.sendForgetAuto(sendType: 2, tfNum: tfNum)
        SendAutoModel.shared.sendForgetAuto(sendType: 2, tfNum: tfNum) { (result) in
            
        }
    }
    
}

